<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <j-dict-select-tag type="list" v-model="model.projectId" dictCode="ASSESSMENT_PROJECTS" placeholder="请选择类型" :disabled=true />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="计分方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="calculationType">
              <j-dict-select-tag type="list" v-model="model.calculationType" dictCode="CALCULATION_TYPE" placeholder="请选择计分方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" v-if="this.showClass">
            <a-form-model-item label="计分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="className">
              <a-input v-model="model.className" placeholder="请输入计分类名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="显示顺序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="className">
              <a-input-number v-model="model.orderNum" placeholder="请输入顺序"  ></a-input-number>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'AssessmentProjectsClassificationForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    projectId :{
      type:String,
      required:true
    }

  },
  data () {
    return {
      model:{
        projectId : this.projectId
      },
      projectIdDis:true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/assessmentProjectsClassification/add",
        edit: "/assessmentProjectsClassification/edit",
        queryById: "/assessmentProjectsClassification/queryById"
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
    showClass(){

      return this.model.calculationType ==2
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },

    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,this.model,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>